package com.qiyou.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IUserDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.User;
import com.qiyou.entity.UserInfo;

/**
 * 用户注册业务逻辑
 */
@WebServlet(name = "UserRegist", urlPatterns = { "/UserRegist" })
public class UserRegistServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String checkCode = request.getParameter("checkcode");
        String validateCode = (String) request.getSession().getAttribute(
                "validateCode");

        if (validateCode == null || !validateCode.equalsIgnoreCase(checkCode)) {
            response.getWriter()
                    .print("<script type='text/javascript'>alert('验证码错误!');history.go(-1);</script>");
            return;
        }

        String userID = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");

        User user = new User();
        user.setID(userID);
        user.setPassword(password);

        IUserDao dao = DaoFactory.getUserDao();
        User queryUser = null;
        try {
            queryUser = dao.getUser(userID);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 没有此用户，可以注册
        if (queryUser == null) {
            UserInfo userInfo = new UserInfo();
            userInfo.setID(userID);
            userInfo.setEmail(email);

            dao = DaoFactory.getUserDao();

            boolean isSuccess = false;
            try {
                isSuccess = dao.doCreate(user, userInfo);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            if (isSuccess) {
                request.getSession().setAttribute("user", user);
                request.getRequestDispatcher("index.jsp").forward(request,
                        response);
            } else {
                response.getWriter()
                        .print("<script type='text/javascript'>alert('注册失败，请稍候再试!');history.go(-1);</script>");
            }
        } else {
            response.getWriter()
                    .print("<script type='text/javascript'>alert('此帐号已经存在，请重新输入一个账号!');history.go(-1);</script>");
        }

    }
}
